package com.yto.hrreport.event.buttons {
	
	import com.yto.hrreport.event.buttons.superbutton.ButtonEvent;
	import com.yto.hrreport.pub.IProcessor;
	
	import mx.containers.Canvas;
	import mx.containers.HDividedBox;
	import mx.containers.Panel;
	import mx.controls.Alert;
	import mx.controls.CheckBox;
	import mx.controls.TextInput;
	import mx.controls.Tree;
	import mx.managers.PopUpManager;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;
	
	public class SysSaveButtonEvent extends ButtonEvent {
		
		private static var saveEvent : SysSaveButtonEvent = new SysSaveButtonEvent();
		private static var parentName : String;
		private var headCanvas : Canvas;
		
		private var txtNodeCode : TextInput;
		private var txtNodeId : TextInput;
		private var txtNodeName : TextInput;
		private var txtParentNodeCode : TextInput;
		private var chkIsParent : CheckBox;
		private var sysTree : Tree;
		
		public function SysSaveButtonEvent() {
			super();
		}
		
		public static function getInstance(parentName : String) : SysSaveButtonEvent {
			
			SysSaveButtonEvent.parentName = parentName;
			return saveEvent;
		}
		
		public function saveEvent() : void {
			
			var parentCanvas : Canvas = Canvas(hrreport.ui.mainNavigator.getChildByName(parentName + "_canvas"));
			headCanvas = Canvas(parentCanvas.getChildByName(parentName + "_headCanvas"));
			var bodyCanvas : Canvas = Canvas(parentCanvas.getChildByName(parentName + "_bodyCanvas"));
			var hdivBox : HDividedBox = HDividedBox(bodyCanvas.getChildByName(parentName + "_hdivBox"));
			sysTree = Tree(hdivBox.getChildByName(parentName + "_sysTree"));
			var sysPanel : Panel = Panel(hdivBox.getChildByName(parentName + "_sysFunc"));
			
			if(sysPanel == null) {
				sysPanel  = Panel(hdivBox.getChildByName(parentName + "_sysTemplate"));
			} else {
				var sysCanvas : Canvas = Canvas(sysPanel.getElementAt(0));
				
				txtNodeCode = TextInput(sysCanvas.getChildByName(parentName + "_txtNodeCode"));
				txtNodeId = TextInput(sysCanvas.getChildByName(parentName + "_txtNodeId"));
				txtNodeName = TextInput(sysCanvas.getChildByName(parentName + "_txtNodeName"));
				txtParentNodeCode = TextInput(sysCanvas.getChildByName(parentName + "_txtParentNodeCode"));
				chkIsParent = CheckBox(sysCanvas.getChildByName(parentName + "_chkIsParent"));
				
				var state : String = sysTree.selectedItem.state;
				
				var nodeServer : RemoteObject = new RemoteObject;
				nodeServer.destination = IProcessor.NodeProcessor;
				
				if(state == "edit") {
					nodeServer.modifyNode(sysTree.selectedItem.pk_nodes , txtNodeCode.text , txtNodeId.text , txtNodeName.text , txtParentNodeCode.text , chkIsParent.selected ? "Y" : "N");
					nodeServer.addEventListener(ResultEvent.RESULT , save);
					
				} else if(state == "add") {
					nodeServer.addNode(txtNodeCode.text , txtNodeId.text , txtNodeName.text , txtParentNodeCode.text , chkIsParent.selected ? "Y" : "N");
					nodeServer.addEventListener(ResultEvent.RESULT , save);
					
				}
				
			}
			
		}
		
		private function save(event : ResultEvent) : void {
			
			var result : String = event.result as String;
			if(result == "1") {
				sysTree.enabled = true;
				txtNodeCode.enabled = false;
				txtNodeId.enabled = false;
				txtNodeName.enabled = false;
				txtParentNodeCode.enabled = false;
				chkIsParent.enabled = false;
				
				updateButtons(null , "sysEdit" , headCanvas , false); 
				RefreshButtonEvent.getInstance(parentName).refreshEvent();
				
			} else {
				Alert.show("节点编码已存在（或未知错误），保存失败！" , "提示" , Alert.OK , hrreport.ui);
			}
		}
		
	}
}